package com.longcai.piaojia.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.OrderListAdapter;
import com.longcai.piaojia.beans.TicketEntity;
import com.longcai.piaojia.conn.EvaluateSubmitPost;
import com.longcai.piaojia.conn.GoodsInfoGet;
import com.longcai.piaojia.conn.NoSeatOrderCancelGet;
import com.longcai.piaojia.conn.OrderCancleGet;
import com.longcai.piaojia.conn.OrderDetailGet;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.longcai.piaojia.fragment.FragmentTicketOrder;
import com.longcai.piaojia.util.MyToast;
import com.longcai.piaojia.util.UtilPermission;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.Http;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilApp;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/6.
 */
public class MyOrderDetailActivity extends BaseActivity implements View.OnClickListener{


    @BoundView(R.id.order_nslv) private ListView order_nslv;
    //订单的 title,时间、地点、多少张票、票面总价
    @BoundView(R.id.submit_order_iv) private ImageView submit_order_iv;
    @BoundView(R.id.order_title_tv) private TextView order_title_tv;
    @BoundView(R.id.order_time_tv) private TextView order_time_tv;
    @BoundView(R.id.order_place_tv) private TextView order_place_tv;
    @BoundView(R.id.order_address_tv) private TextView order_address_tv;
    @BoundView(R.id.order_top_order_count_num_tv) private TextView order_top_order_count_num_tv;
    @BoundView(R.id.order_top_order_money_tv) private TextView order_top_order_money_tv;
    // 订单编号、下单时间、订单状态
    @BoundView(R.id.myorder_detail_num_tv) private TextView myorder_detail_num_tv;
    @BoundView(R.id.myorder_detail_order_time_tv) private TextView myorder_detail_order_time_tv;
    @BoundView(R.id.myorder_detail_order_state_tv) private TextView myorder_detail_order_state_tv;
    @BoundView(R.id.myorder_detail_order_ticket_num_tv) private TextView myorder_detail_order_ticket_num_tv;
    @BoundView(R.id.myorder_detail_order_show_time_tv) private TextView myorder_detail_order_show_time_tv;
    @BoundView(R.id.myorder_detail_order_ticket_num_ll) private LinearLayout myorder_detail_order_ticket_num_ll;
    //配送方式布局里 的 textview
    @BoundView(R.id.myorder_detail_shipping_type_tv) private TextView myorder_detail_shipping_type_tv;
    //配送方式-快递方式的布局,姓名、电话、收货地址
    @BoundView(R.id.dispatching_type_express_ll) private LinearLayout dispatching_type_express_ll;
    @BoundView(R.id.dispatching_type_express_name_tv) private TextView dispatching_type_express_name_tv;
    @BoundView(R.id.dispatching_type_express_phone_tv) private TextView dispatching_type_express_phone_tv;
    @BoundView(R.id.dispatching_type_express_address_tv) private TextView dispatching_type_express_address_tv;
    //配送方式-上门自取的布局，姓名，电话号，取票地址
    @BoundView(R.id.dispatching_type_since_ll) private LinearLayout dispatching_type_since_ll;
    @BoundView(R.id.dispatching_type_consignee_tv) private TextView dispatching_type_consignee_tv;
    @BoundView(R.id.dispatching_type_phone_tv) private TextView dispatching_type_phone_tv;
    @BoundView(R.id.dispatching_type_ticket_address_tv) private TextView dispatching_type_ticket_address_tv;

    //优惠券的  布局 和 textview
    @BoundView(R.id.myorder_detail_coupon_rl) private RelativeLayout myorder_detail_coupon_rl;
    @BoundView(R.id.myorder_detail_coupon_tv) private TextView myorder_detail_coupon_tv;
    @BoundView(R.id.myorder_detail_count_tv) private TextView myorder_detail_count_tv;
    //积分使用的布局, button的图片
    @BoundView(R.id.order_integration_rl) private RelativeLayout order_integration_rl;
    @BoundView(R.id.order_integration_check_sel_iv) private ImageView order_integration_check_sel_iv;
    @BoundView(R.id.order_integration_check_no_iv) private ImageView order_integration_check_no_iv;
    @BoundView(R.id.order_integration_tv) private TextView order_integration_tv;
    @BoundView(R.id.order_integration_deduction_tv) private TextView order_integration_deduction_tv;
    //发票使用的布局 和 button图片
    @BoundView(R.id.order_draw_bill_rl) private RelativeLayout order_draw_bill_rl;
    @BoundView(R.id.order_draw_bill_check_sel_iv) private ImageView order_draw_bill_check_sel_iv;
    @BoundView(R.id.order_draw_bill_check_no_iv) private ImageView order_draw_bill_check_no_iv;

    //订单详细
    @BoundView(R.id.submit_order_bottom_price_tv) private TextView submit_order_bottom_price_tv;
    @BoundView(R.id.submit_order_bottom_freight_price_tv) private TextView submit_order_bottom_freight_price_tv;
    @BoundView(R.id.submit_order_bottom_coupon_price_tv) private TextView submit_order_bottom_coupon_price_tv;
    @BoundView(R.id.submit_order_bottom_integral_price_tv) private TextView submit_order_bottom_integral_price_tv;
    @BoundView(R.id.submit_order_bottom_total_price_tv) private TextView submit_order_bottom_total_price_tv;

    //未支付状态下的 订单提交按钮 布局
    @BoundView(R.id.submit_order_bottom_rl) private RelativeLayout submit_order_bottom_rl;
    @BoundView(R.id.submit_order_bottom_cancel_tv) private TextView submit_order_bottom_cancel_tv;
    @BoundView(R.id.submit_order_bottom_submit_tv) private TextView submit_order_bottom_submit_tv;
    //已支付显示的按钮 布局
    @BoundView(R.id.myorder_detail_submit_ll) private LinearLayout myorder_detail_submit_ll;
    @BoundView(R.id.myorder_detail_ispay_tv) private TextView myorder_detail_ispay_tv;
    @BoundView(R.id.myorder_detail_ll) private LinearLayout myorder_detail_ll;

    private OrderListAdapter orderListAdapter;
    private List<TicketEntity> lists = new ArrayList<>();

    private GoodsInfoGet.GoodsInfoEntity goodsInfo;
    private OrderDetailGet.OrderDetailEntity orderDetail;

    private boolean isPayed = false;

    private boolean isShowDialog; //记录提示拨打电话弹框是否显示过

    //无座订单取消接口
    private NoSeatOrderCancelGet noSeatOrderCancelGet = new NoSeatOrderCancelGet(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

            MyToast.show(context, s, MyToast.LENGTH_SHORT);

            try {
                if(!isPayed){

                    FragmentTicketOrder.RefreshOrderList refreshOrderList = (FragmentTicketOrder.RefreshOrderList) getAppCallBack(FragmentTicketOrder.class);
                    refreshOrderList.refresh();

                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.show(context, type, MyToast.LENGTH_SHORT);
        }
    });

    //有座 取消订单接口
    private OrderCancleGet orderCancleGet = new OrderCancleGet(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            MyToast.show(context, toast, MyToast.LENGTH_SHORT);

            try {
                if(!isPayed){
                    FragmentTicketOrder.RefreshOrderList refreshOrderList = (FragmentTicketOrder.RefreshOrderList) getAppCallBack(FragmentTicketOrder.class);
                    refreshOrderList.refresh();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            finish();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.show(context, toast, MyToast.LENGTH_SHORT);
        }
    });

    private OrderDetailGet orderDetailGet = new OrderDetailGet(new AsyCallBack<OrderDetailGet.OrderDetailEntity>(){

        @Override
        public void onSuccess(String toast, int type, OrderDetailGet.OrderDetailEntity orderDetailEntity) throws Exception {

            orderDetail = orderDetailEntity;

            //头部分
            GlideLoader.getInstance().get(context, orderDetailEntity.orderShowDetail.picurl, submit_order_iv, R.mipmap.goods);
            order_title_tv.setText(orderDetailEntity.orderShowDetail.title);
            order_time_tv.setText(getString(R.string.goods_info_show_time_title_text) + orderDetailEntity.orderShowDetail.posttime);
            order_place_tv.setText(getString(R.string.goods_info_show_address_title_text) + orderDetailEntity.orderShowDetail.place);
            order_address_tv.setText(getString(R.string.goods_info_show_place_title_text) + orderDetailEntity.orderShowDetail.address);
            order_top_order_count_num_tv.setText(orderDetailEntity.ballot + getString(R.string.mybag_ticket_count_text));
            order_top_order_money_tv.setText(orderDetailEntity.total_money + getString(R.string.goods_info_money_text));

            switch (orderDetailEntity.orderAddress.type) {

                case "配送方式-快递":

                    dispatching_type_since_ll.setVisibility(View.GONE);
                    dispatching_type_express_ll.setVisibility(View.VISIBLE);

                    myorder_detail_shipping_type_tv.setText(orderDetailEntity.orderAddress.type);

                    dispatching_type_express_name_tv.setText(orderDetailEntity.orderAddress.name);
                    dispatching_type_express_phone_tv.setText(orderDetailEntity.orderAddress.phone);
                    dispatching_type_express_address_tv.setText(orderDetailEntity.orderAddress.address);

                    break;

                case "配送方式-上门自取":

                    dispatching_type_since_ll.setVisibility(View.VISIBLE);
                    dispatching_type_express_ll.setVisibility(View.GONE);

                    myorder_detail_shipping_type_tv.setText(orderDetailEntity.orderAddress.type);

                    dispatching_type_consignee_tv.setText(orderDetailEntity.orderAddress.name);
                    dispatching_type_phone_tv.setText(orderDetailEntity.orderAddress.phone);
                    dispatching_type_ticket_address_tv.setText(orderDetailEntity.orderAddress.address);

                    break;

                case "配送方式-现场兑票":

                    dispatching_type_since_ll.setVisibility(View.GONE);
                    dispatching_type_express_ll.setVisibility(View.GONE);
                    myorder_detail_shipping_type_tv.setText(orderDetailEntity.orderAddress.type);

                    break;

            }

            Log.e("----money->", orderDetailEntity.orderCouponList.size() + "");

            if (orderDetailEntity.orderCouponList.size() > 0) {

                BigDecimal couponMoney = new BigDecimal("0");

                for (int i = 0; i < orderDetailEntity.orderCouponList.size(); i++) {

                    couponMoney = couponMoney.add(new BigDecimal(orderDetailEntity.orderCouponList.get(i).money));

                }

                myorder_detail_coupon_rl.setVisibility(View.VISIBLE);
                myorder_detail_coupon_tv.setText(getString(R.string.submit_order_coupon_benefit_text) + couponMoney + getString(R.string.goods_info_money_text));//.setScale(0, BigDecimal.ROUND_HALF_UP)
                myorder_detail_count_tv.setText(getString(R.string.submit_order_coupon_count_symbols_text)+ orderDetailEntity.orderCouponList.size() +getString(R.string.submit_order_coupon_count_text));

                submit_order_bottom_coupon_price_tv.setText(getString(R.string.goods_info_symbols_text) + couponMoney + getString(R.string.goods_info_money_text));//.setScale(0, BigDecimal.ROUND_HALF_UP)

            } else {

                submit_order_bottom_coupon_price_tv.setText("0" + getString(R.string.goods_info_money_text));
            }


            Log.e("----type->", orderDetailEntity.orderIntegral.type);
            //积分实体 中 类别为空就不显示积分布局
            if (!TextUtils.isEmpty(orderDetailEntity.orderIntegral.type)) {

                order_integration_rl.setVisibility(View.VISIBLE);
                order_integration_check_sel_iv.setVisibility(View.VISIBLE);
                order_integration_check_no_iv.setVisibility(View.GONE);

                order_integration_tv.setText(orderDetailEntity.orderIntegral.integral + getString(R.string.mine_count_unit));
                order_integration_deduction_tv.setText(getString(R.string.submit_order_integral_text)+ orderDetailEntity.orderIntegral.discount + getString(R.string.goods_info_money_text));

                submit_order_bottom_integral_price_tv.setText(getString(R.string.goods_info_symbols_text) + orderDetailEntity.orderIntegral.discount + getString(R.string.goods_info_money_text));

            } else {

                submit_order_bottom_integral_price_tv.setText("0" + getString(R.string.goods_info_money_text));

            }

            //发票

            if (orderDetailEntity.invoice.equals("0")) {

                order_draw_bill_check_no_iv.setVisibility(View.VISIBLE);
                order_draw_bill_check_sel_iv.setVisibility(View.GONE);
            } else {

                order_draw_bill_check_no_iv.setVisibility(View.GONE);
                order_draw_bill_check_sel_iv.setVisibility(View.VISIBLE);
            }


            //订单详细
            submit_order_bottom_price_tv.setText(orderDetailEntity.total_money + getString(R.string.goods_info_money_text));
            submit_order_bottom_total_price_tv.setText(orderDetailEntity.discount_price + getString(R.string.goods_info_money_text));
            submit_order_bottom_freight_price_tv.setText(orderDetailEntity.postage + getString(R.string.goods_info_money_text));


            // 订单编号、下单时间、订单状态
            myorder_detail_num_tv.setText(orderDetailEntity.order_number);
            myorder_detail_order_time_tv.setText(orderDetailEntity.posttime);
            myorder_detail_order_show_time_tv.setText(orderDetailEntity.times);

            if(isPayed){

                myorder_detail_order_state_tv.setText(getString(R.string.myorder_list_state_payed_text));

                submit_order_bottom_rl.setVisibility(View.GONE);
                myorder_detail_submit_ll.setVisibility(View.GONE);

                myorder_detail_ll.setPadding(0, 0, 0, 0);

            }else{

                switch (Integer.parseInt(orderDetailEntity.status)) {

                    case 0:

                        //订单状态
                        myorder_detail_order_state_tv.setText(getString(R.string.myorder_list_state_no_pay_text));

                        submit_order_bottom_rl.setVisibility(View.VISIBLE);
                        submit_order_bottom_cancel_tv.setVisibility(View.VISIBLE);//取消订单按钮
                        myorder_detail_submit_ll.setVisibility(View.GONE);

                        myorder_detail_ll.setPadding(0, 0, 0, ScaleScreenHelperFactory.getInstance().getWidthHeight(110));

                        break;

                    case 1:

                        //订单状态
                        myorder_detail_order_state_tv.setText(getString(R.string.myorder_list_state_payed_text));

                        submit_order_bottom_rl.setVisibility(View.GONE);
                        myorder_detail_submit_ll.setVisibility(View.GONE);

                        myorder_detail_ll.setPadding(0, 0, 0, 0);

                        break;

                    case 2:

                        //订单状态
                        myorder_detail_order_state_tv.setText(getString(R.string.myorder_list_state_used_text));

                        myorder_detail_ispay_tv.setText(getString(R.string.myorder_detail_goto_envaluate_text));

                        submit_order_bottom_rl.setVisibility(View.GONE);
                        myorder_detail_submit_ll.setVisibility(View.VISIBLE);

                        myorder_detail_ll.setPadding(0, 0, 0, ScaleScreenHelperFactory.getInstance().getWidthHeight(110));

                        break;

                    case 3:

                        //订单状态
                        myorder_detail_order_state_tv.setText(getString(R.string.myorder_detail_haspay_text));

                        submit_order_bottom_rl.setVisibility(View.GONE);
                        myorder_detail_submit_ll.setVisibility(View.GONE);

                        break;

                    case 4:

                        //订单状态
                        myorder_detail_order_state_tv.setText(getString(R.string.myorder_detail_haspay_text));

                        submit_order_bottom_rl.setVisibility(View.GONE);
                        myorder_detail_submit_ll.setVisibility(View.GONE);

                        break;

                    case 5:
                        //订单状态
                        myorder_detail_order_state_tv.setText(getString(R.string.myorder_detail_haspay_text));

                        submit_order_bottom_rl.setVisibility(View.GONE);
                        myorder_detail_submit_ll.setVisibility(View.GONE);

                        break;

                    case 6:

                        //订单状态
                        myorder_detail_order_state_tv.setText(getString(R.string.myorder_list_state_pay_complete_text));

                        submit_order_bottom_rl.setVisibility(View.GONE);
                        myorder_detail_submit_ll.setVisibility(View.GONE);

                        myorder_detail_ll.setPadding(0, 0, 0, 0);

                        break;


                }

            }

            if(orderDetailEntity.type.equals("1")){

                String content[] = orderDetailEntity.content.split(",");
                String price[] = orderDetailEntity.money.split(",");

                for(int i=0;i<content.length;i++){

                    //票列表 数据源
                    TicketEntity ticketEntity = new TicketEntity();

//                    String[] timeArr = orderDetailEntity.times.split(",");
                    ticketEntity.count = content[i];
                    ticketEntity.time = orderDetailEntity.times;
                    ticketEntity.singlePrice = price[i];

                    lists.add(ticketEntity);

                }

                if (orderDetailEntity.status.equals("2") || orderDetailEntity.status.equals("6")) {

                    myorder_detail_order_ticket_num_ll.setVisibility(View.VISIBLE);

                    myorder_detail_order_ticket_num_tv.setText(orderDetailEntity.orderNo);

                }

            }else{

                //票列表 数据源
                TicketEntity ticketEntity = new TicketEntity();

//                String[] timeArr = orderDetailEntity.times.split(",");
                ticketEntity.count = "1" + getString(R.string.mybag_ticket_count_text);
                ticketEntity.time = orderDetailEntity.times;
                ticketEntity.singlePrice = orderDetailEntity.money;

//                BigDecimal totalPrice = new BigDecimal(orderDetailEntity.total_money);
//                BigDecimal totalCount = new BigDecimal(orderDetailEntity.ballot);
//                ticketEntity.singlePrice = totalPrice.divide(totalCount).toString();

                lists.add(ticketEntity);

            }

            orderListAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {


        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_myorder_detail);

        String[] permission = {Manifest.permission.CALL_PHONE};
        UtilPermission.Permission(this, permission);

        isPayed = getIntent().getBooleanExtra("isPayed", false);

        Log.v("maning", isPayed+"");

        setBackTrue();

        setTitleName(getString(R.string.myorder_detail_title_text));

        order_nslv.setDividerHeight(ScaleScreenHelperFactory.getInstance().getWidthHeight(20));

        order_nslv.setAdapter(orderListAdapter = new OrderListAdapter(this, lists));

        orderDetailGet.order_number = getIntent().getStringExtra("order_number");
        orderDetailGet.execute(this);

        submit_order_bottom_cancel_tv.setOnClickListener(this);
        submit_order_bottom_submit_tv.setOnClickListener(this);
        myorder_detail_ispay_tv.setOnClickListener(this);

        setAppCallBack(new RefreshDetail());

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.submit_order_bottom_cancel_tv:

                new WarnningDialog(context, context.getString(R.string.goods_info_bottom_cancel_text)) {
                    @Override
                    protected void onConfirm() {

                        submit_order_bottom_submit_tv.setEnabled(false);

                        if (orderDetail.type.equals("1")) {

                            orderCancleGet.orderId = orderDetail.orderId;
                            orderCancleGet.orderNo = orderDetail.orderNo;
                            orderCancleGet.execute(MyOrderDetailActivity.this);

                        } else {

                            noSeatOrderCancelGet.order_number = orderDetail.order_number;
                            noSeatOrderCancelGet.ballot = orderDetail.ballot;
                            noSeatOrderCancelGet.play_id = orderDetail.play_id;
                            noSeatOrderCancelGet.execute(MyOrderDetailActivity.this);

                        }

                    }
                }.show();

                break;

            case R.id.submit_order_bottom_submit_tv:


                if (!orderDetail.type.equals("1") && !isShowDialog) {

                    new WarnningDialog(context, getString(R.string.warnning_call_service_text), context.getString(R.string.cancel_text), getString(R.string.call_text)) {
                        @Override
                        protected void onConfirm() {

                            new WarnningDialog(context, BaseApplication.BasePreferences.readNoSeatServiceTel(), context.getString(R.string.cancel_text), context.getString(R.string.confirm_text)) {
                                @Override
                                protected void onConfirm() {

                                    UtilApp.call(context, BaseApplication.BasePreferences.readNoSeatServiceTel());

                                    isShowDialog = true;

                                    return;
                                }
                            }.show();

                        }
                    }.show();

                } else {

                    isShowDialog = true;
                }



                Bundle bundle = new Bundle();
                bundle.putString("orderNum", getIntent().getStringExtra("order_number"));
                bundle.putString("truePrice", orderDetail.discount_price);
                bundle.putString("title", orderDetail.orderShowDetail.title);
                bundle.putString("classid", orderDetail.classid);
                bundle.putString("orderId", orderDetail.orderId);
                bundle.putString("orderNo", orderDetail.orderNo);
                bundle.putBoolean("isOrder", true);
                bundle.putString("ordertotalAmount", orderDetail.ordertotalAmount);
                bundle.putString("is_balance", orderDetail.is_balance);
                bundle.putString("postage", orderDetail.postage);

                if (isShowDialog) {

                    startActivity(new Intent(MyOrderDetailActivity.this, PayActivity.class).putExtras(bundle));

                }

                break;

            case R.id.myorder_detail_ispay_tv:


                //演出实体
                goodsInfo = new GoodsInfoGet.GoodsInfoEntity();

                goodsInfo.id = orderDetail.show_id;
                goodsInfo.title = orderDetail.orderShowDetail.title;
                goodsInfo.picurl = orderDetail.orderShowDetail.picurl;

                startActivity(new Intent(MyOrderDetailActivity.this, GoodsEvaluateActivity.class).putExtra("goodsInfo", goodsInfo).putExtra("order_number", getIntent().getStringExtra("order_number")));

                break;


        }


    }


    public class RefreshDetail implements AppCallBack {

        public void refresh(){

            myorder_detail_order_state_tv.setText(getString(R.string.myorder_list_state_payed_text));

            submit_order_bottom_rl.setVisibility(View.GONE);
            myorder_detail_submit_ll.setVisibility(View.GONE);

            myorder_detail_ll.setPadding(0, 0, 0, 0);

        }

        public void afterEvaluateRefresh() {

            lists.clear();

            orderDetailGet.order_number = getIntent().getStringExtra("order_number");
            orderDetailGet.execute(MyOrderDetailActivity.this);

            submit_order_bottom_rl.setVisibility(View.GONE);
            myorder_detail_submit_ll.setVisibility(View.GONE);

        }

    }



}
